package org.csu.mypetstore.web.servlets;

import org.csu.mypetstore.domain.Order;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.DataInput;
import java.io.IOException;
import java.util.Date;

public class ConfirmOrderActionServlet extends HttpServlet {

    public static final String VIEW_CONFIRMORDER = "/WEB-INF/jsp/order/ConfirmOrder.jsp";
    public static final String VIEW_SHIPPINGFORM = "/WEB-INF/jsp/order/ShippingForm.jsp";
    private Order order;
    private Date date;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        HttpSession session = request.getSession();
        order = (Order)session.getAttribute("order");

        order.setCardType(request.getParameter("order.cardType"));
        order.setCreditCard(request.getParameter("order.creditCard"));
        order.setExpiryDate(request.getParameter("order.expiryDate"));
        order.setBillToFirstName(request.getParameter("order.billToFirstName"));
        order.setBillToLastName(request.getParameter("order.billToLastName"));
        order.setBillAddress1(request.getParameter("order.billAddress1"));
        order.setBillAddress2(request.getParameter("order.billAddress2"));
        order.setBillCity(request.getParameter("order.billCity"));
        order.setBillState(request.getParameter("order.billState"));
        order.setBillZip(request.getParameter("order.billZip"));
        order.setBillCountry(request.getParameter("order.billCountry"));


        session.setAttribute("order", order);

        if (request.getParameter("shippingAddressRequired") == null) {
            request.getRequestDispatcher(VIEW_CONFIRMORDER).forward(request, response);
        }else{
            request.getRequestDispatcher(VIEW_SHIPPINGFORM).forward(request, response);
        }
    }
}
